package edu.tyut.game.ui.user.password

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import edu.tyut.game.databinding.ActivityFrogetPasswordBinding
import edu.tyut.game.ui.user.SharedViewModel
import edu.tyut.game.utils.LogUtils
import edu.tyut.game.utils.StatusBarUtils

class ForgetPasswordActivity : AppCompatActivity() {

    private val tag = "ForgetPasswordActivity"

    private lateinit var binding : ActivityFrogetPasswordBinding

    private val viewModel by lazy {
        ViewModelProvider(this)[ForgetPasswordViewModel::class.java]
    }

    private val sharedViewModel by lazy {
        ViewModelProvider(this)[SharedViewModel::class.java]
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        binding = ActivityFrogetPasswordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 隐藏ActionBar
        // 状态栏透明
        StatusBarUtils.setTranslucentStatusBar(this)

        // 验证码
        var webCode: String? = null
        viewModel.sendMailData.observe(this) { result ->
            if (result.isSuccess) {
                webCode = result.getOrNull()
                LogUtils.d(tag, "找回密码验证码：$webCode")
            }
        }

        // 获取需要找回密码的用户，先赋值viewModel user
        viewModel.retrievePasswordUser.observe(this){ result ->
            if (result.isSuccess){
                val user = result.getOrNull()
                if (user != null){
                    viewModel.user = user
                    if (user.phone == null){
                        binding.emailRetrieve.isChecked = true
                        binding.phoneRetrieve.isEnabled = false
                        // 发送邮箱
                        viewModel.sendRetrievePasswordEmail(user)
                        LogUtils.d(tag, "邮箱发送成功")
                    }else{
                        // 如果电话号码不为空
                        if (binding.emailRetrieve.isChecked){
                            // 发送邮箱
                            viewModel.sendRetrievePasswordEmail(user)
                        }else{
                            // 发送电话号码
                            LogUtils.d(tag, "TODO 发送电话号码")
                            Toast.makeText(this, "😥抱歉目前尚不支持手机号码找回密码", Toast.LENGTH_LONG).show()
                        }
                    }
                    LogUtils.d(tag, "viewModel: $user")
                }else{
                    Toast.makeText(this, "用户名不存在!", Toast.LENGTH_SHORT).show()
                }
            }
        }

        // 获取验证码
        sharedViewModel.second.observe(this){
            binding.getVerificationCode.text = "${it}s"
        }

        binding.getVerificationCode.setOnClickListener {

            val button = it as Button

            val username = binding.username.text.toString().trim()

            if (username.isNotEmpty()) {
                viewModel.getUser(username)
                sharedViewModel.countDown(60000, button)
            } else {
                Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show()
                // return@setOnClickListener
            }
        }

        //
        binding.next.setOnClickListener {
            val username = binding.username.text.toString().trim()
            if (username.isEmpty()) {
                Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val verificationCode = binding.verificationCode.text.toString().trim()
            if (verificationCode.isEmpty()){
                Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (verificationCode != webCode){
                Toast.makeText(this, "验证码不正确", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            Toast.makeText(this, "验证码正确", Toast.LENGTH_SHORT).show()
            ConfirmPasswordActivity.actionStart(this, viewModel.user)
        }

    }
    // 别的Activity启动LoginActivity
    companion object{
        fun actionStart(context: Context){
            val intent = Intent(context, ForgetPasswordActivity::class.java)
            context.startActivity(intent)
        }
    }
}